package com.shop.advertisement.service.impl;

import com.shop.advertisement.service.IndexRecommendAdService;
import com.shop.api.feign.IndexRecommendAdFeign;
import com.shop.dao.LdbzIndexRecommendAdMapper;
import com.shop.pojo.LdbzIndexRecommendAd;
import com.shop.utils.FastJsonConvert;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IndexRecommendAdServiceImpl implements IndexRecommendAdService {
    private static final Logger logger = LoggerFactory.getLogger(IndexRecommendAdServiceImpl.class);

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 索引推荐信息键
     */
    @Value("${redisKey.indexRecommend.info.key}")
    private String INDEX_RECOMMEND_INFO_KEY;
    /**
     * redis过期时间
     */
    @Value("${redisKey.expire_time}")
    private Integer REDIS_EXPIRE_TIME;

    @Autowired
    LdbzIndexRecommendAdMapper mapper;

    /**
     * 获取索引推荐广告页面
     *
     * @param entity
     * @param start
     * @param limit
     * @return
     */
    @Override
    public List<LdbzIndexRecommendAd> getIndexRecommendAdPage(LdbzIndexRecommendAd entity, int start, int limit) {
        logger.debug("getIndexRecommendAdPage {} ，{} ，{}", entity, start, limit);
        return mapper.getIndexRecommendAdPage(entity,start, limit);
    }

    /**
     * 获取索引推荐广告数量
     *
     * @param entity
     * @return
     */
    @Override
    public long countByEntity(LdbzIndexRecommendAd entity) {
        logger.debug("countByEntity {}", entity);
        return mapper.countByEntity(entity);
    }

    /**
     * 根据id获取索引推荐广告
     *
     * @param id
     * @return
     */
    @Override
    public LdbzIndexRecommendAd selectByKey(Long id) {
        logger.debug("selectByKey {}", id);
        return mapper.selectByKey(id);
    }

    /**
     * 更新索引推荐广告
     *
     * @param entity
     * @return
     */
    @Override
    public int updateByKey(LdbzIndexRecommendAd entity) {
        logger.debug("updateByKey {}", entity);
        return mapper.updateByKey(entity);
    }

    /**
     * 在redis中获取索引推荐广告
     *
     * @return
     */
    @Override
    public List<LdbzIndexRecommendAd> getIndexRecommendAdByRedis() {
        logger.debug("getIndexRecommendAdByRedis");
        try {
            String json = String.valueOf(redisTemplate.opsForValue().get(INDEX_RECOMMEND_INFO_KEY));
            if (StringUtils.isNotBlank(json)) {
                logger.info("Redis 获取首页广告信息");
                return FastJsonConvert.convertJSONToArray(json, LdbzIndexRecommendAd.class);
            } else {
                logger.error("Redis 获取首页广告信息失败,查询不到首页广告");
            }
        } catch (Exception e) {
            logger.error("Redis 获取首页广告缓存信息报错", e);
        }

        List<LdbzIndexRecommendAd> ret = mapper.selectByEntity(new LdbzIndexRecommendAd(), 0, Integer.MAX_VALUE);
        try {
            logger.info("Redis 更新首页广告信息");
            redisTemplate.opsForValue().set(INDEX_RECOMMEND_INFO_KEY, FastJsonConvert.convertObjectToJSON(ret));
            redisTemplate.getExpire(INDEX_RECOMMEND_INFO_KEY);
        } catch (Exception e) {
            logger.error("Redis 更新首页广告缓存信息报错", e);
        }
        return ret;
    }
}
